package com.fjec.common.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;

import java.util.List;

public final class AccountInfo {

    /*--------------------------------------------
    |             C O N S T A N T S             |
    ============================================*/
    public static final String ATTR_PREFIX = AccountInfo.class.getName();

    public static final String SESSION_ATTR_USER_ID = ATTR_PREFIX + ".userId";

    public static final String SESSION_ATTR_USER_OPENID = ATTR_PREFIX + ".openId";

    public static final String SESSION_ATTR_USER_ROLE = ATTR_PREFIX + ".role";

    public static final String SESSION_ATTR_USER_BUSI_ID = ATTR_PREFIX + ".busiId";

    public static final String SESSION_ATTR_USER_BUSI_TYPE = ATTR_PREFIX + ".busiType";

    public static final String SESSION_ATTR_USER_SHOP_NAME = ATTR_PREFIX + ".shopName";

    public static final String SESSION_ATTR_USER_WX_NICKNAME = ATTR_PREFIX + ".wxNickName";

    public static final String SESSION_ATTR_USER_HEADPIC_URL = ATTR_PREFIX + ".headPicUrl";

    public static final String SESSION_ATTR_USER_NAME = ATTR_PREFIX + ".userName";


    /**
     * 初始化账户信息
     *
     * @param userId 用户ID
     * @param openId 用户微信openID
     */
    public static void init(Long userId, String openId, List<String> role, Long busiId, Integer busiType,
                            String shopName, String wxNickName, String headPicUrl) {
        Session session = getSession(true);
        session.setAttribute(SESSION_ATTR_USER_ID, userId);
        session.setAttribute(SESSION_ATTR_USER_OPENID, openId);
        session.setAttribute(SESSION_ATTR_USER_ROLE, role);
        session.setAttribute(SESSION_ATTR_USER_BUSI_ID, busiId);
        session.setAttribute(SESSION_ATTR_USER_BUSI_TYPE, busiType);
        session.setAttribute(SESSION_ATTR_USER_SHOP_NAME, shopName);
        session.setAttribute(SESSION_ATTR_USER_WX_NICKNAME, wxNickName);
        session.setAttribute(SESSION_ATTR_USER_HEADPIC_URL, headPicUrl);
    }

    /**
     * 初始化用户名密码登录账户
     *
     * @param userId
     * @param userName
     * @param role
     */
    public static void init(Long userId, String userName, List<String> role) {
        Session session = getSession(true);
        session.setAttribute(SESSION_ATTR_USER_ID, userId);
        session.setAttribute(SESSION_ATTR_USER_NAME, userName);
        session.setAttribute(SESSION_ATTR_USER_ROLE, role);
    }

    /**
     * 删除账户信息
     */
    public static void clean() {
        Session session = getSession();
        session.removeAttribute(SESSION_ATTR_USER_ID);
        session.removeAttribute(SESSION_ATTR_USER_OPENID);
        session.removeAttribute(SESSION_ATTR_USER_ROLE);
        session.removeAttribute(SESSION_ATTR_USER_BUSI_ID);
        session.removeAttribute(SESSION_ATTR_USER_BUSI_TYPE);
        session.removeAttribute(SESSION_ATTR_USER_SHOP_NAME);
        session.removeAttribute(SESSION_ATTR_USER_WX_NICKNAME);
        session.removeAttribute(SESSION_ATTR_USER_HEADPIC_URL);
        session.removeAttribute(SESSION_ATTR_USER_NAME);
    }

    /**
     * 获取SESSION
     */
    public static Session getSession() {
        Session session = getSession(false);
        return session;
    }

    /**
     * 获取SESSION
     *
     * @param b
     * @return
     */
    public static Session getSession(boolean b) {
        return SecurityUtils.getSubject().getSession(b);
    }

    /**
     * 是否已认证
     *
     * @return
     */
    public static boolean isAuthenticated() {
        return SecurityUtils.getSubject().isAuthenticated();
    }

    /**
     * 获取用户ID
     *
     * @return
     */
    public static Long getUserId() {
        return getAndConvert(SESSION_ATTR_USER_ID, Long.class);
    }

    /**
     * 获取微信OPENID
     *
     * @return
     */
    public static String getOpenId() {
        return getAndConvert(SESSION_ATTR_USER_OPENID, String.class);
    }

    /**
     * 获取角色
     *
     * @return
     */
    public static List<String> getRole() {
        return getAndConvert(SESSION_ATTR_USER_ROLE, List.class);
    }

    /**
     * 获取商户类型
     *
     * @return
     */
    public static Integer getBusiType() {
        return getAndConvert(SESSION_ATTR_USER_BUSI_TYPE, Integer.class);
    }

    /**
     * 商户Id
     *
     * @return
     */
    public static Long getBusiId() {
        return getAndConvert(SESSION_ATTR_USER_BUSI_ID, Long.class);
    }

    /**
     * 店铺名称
     */
    public static String getShopName() {
        return getAndConvert(SESSION_ATTR_USER_SHOP_NAME, String.class);
    }

    /**
     * 微信昵称
     */
    public static String getWxNickName() {
        return getAndConvert(SESSION_ATTR_USER_WX_NICKNAME, String.class);
    }

    /**
     * 头像地址
     */
    public static String getHeadPic() {
        return getAndConvert(SESSION_ATTR_USER_HEADPIC_URL, String.class);
    }

    /**
     * 用户登录名
     *
     * @return
     */
    public static String getUserName() {
        return getAndConvert(SESSION_ATTR_USER_NAME, String.class);
    }

    public static <T> T getAndConvert(String key, Class<T> clazz) {
        Object obj = getSession().getAttribute(key);
        if (obj != null) {
            return (T) obj;
        } else {
            return null;
        }
    }
}
